import wx

import logging

import pyciv.attribute
import pyciv.intel


class Player():
    def __init__(self, color, name):
        self.color = color
        self.name = name
        self.attributes = {pyciv.attribute.SUPPLY:0,
                           pyciv.attribute.VICTORY:0}

    def get_owned_hexes(self, engine):
        my_hexes = []
        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                my_hexes.append(hex)
        return my_hexes

    def before_turn(self, engine):
        engine.calculate_intel()
        for hex in engine.state.hexes:
            if engine.state.owner[hex] == self:
                engine.state.unit[hex].prepare_turn()
                self.attributes[pyciv.attribute.SUPPLY] += engine.state.supply[hex]

    def do_turn(self, engine):
        return True

    def after_turn(self, engine):
        pass

    def is_default_player(self):
        return False

class Default(Player):
    def __init__(self):
        Player.__init__(self, wx.WHITE, "Default")

    def do_turn(self, engine):
        # Default player will be run first every turn (except the first turn)
        # So if you want something to happen once every turn but not connected to player
        # here is the spot
        
        for hex in engine.state.hexes:
            engine.state.owner[hex].attributes[pyciv.attribute.VICTORY] += \
                engine.state.victory[hex]

        # Create a score
        total_score = 0
        score = "Score turn %d:\n" % (engine.state.turn - 1)
        for player in engine.state.players:
            if not player.is_default_player():
                score += "%2d %s\n" % (player.attributes[pyciv.attribute.VICTORY], player.name)
        score += "To win: %d points" % engine.points_to_win()
        engine.combat_results.append(score)

        return True

    def is_default_player(self):
        return True

class Human(Player):
    def __init__(self, color, name):
        Player.__init__(self, color, name)
    def do_turn(self, engine):
        return False

class Computer(Player):
    def __init__(self, color, name):
        Player.__init__(self, color, name)

    def do_turn(self, engine):
        return True

